import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UtilService {
 readImgUrl = 'http://static.lessonplan.cn'; /*访问静态资源图片地址*/
  localStoragyKey = 'record';
  constructor(
    private http: HttpClient,
  ) { }

  getUserInfo(): Promise<any> {
    // return new Promise((resolve, reject) => {
    //   resolve({status: 1, userGuid: '96a5f559-590e-0c9a-8cb9-1f4b1f57f456'})
    // })
    return this.http.get(`/api/userInfo`).toPromise().then(res => {
      return res;
    })
  }

  logout(): Promise<any> {
    return this.http.get(`/logout`).toPromise().then(res => {
      return res;
    })
  }
//  返回当前年份+月份字符串
  getYearAndMonthOfstring(date){
    let year = date.getFullYear();
    let month = date.getMonth()+1;
    return  year+""+month;
  }
  //返回时间戳
  getTimeTamp(date){
    let timeTamp = date.getTime();
    return Math.floor(timeTamp/1000);
  }
  //将时间戳返回成日期格式
  getDateString(timeTamp){
    let date  = new Date(timeTamp+1000);
    return date;
  }
  //返回一个月前的时间戳
  getTimeTampLatly(date){
    let m =date.getMonth();
    m-=1;
    date.setMonth(m);
    return Math.floor(date.getTime()/1000);
  }
  //去除空格
  trim (str){
    return str.replace(/(^\s*)|(\s*$)/g, "");
  }
  //转换日期字符串
  changDateStringOfDay(date){
    let y = date.getFullYear();
    let m = date.getMonth()+1;
    let d = date.getDate();
    return y+'.'+m+'.'+d+'';
  }
  //兼容ios
  changDateString(date){
    date =date.replace(/\-/g, "/");
    let t = new Date(date);
    let y = t.getFullYear();
    let m = t.getMonth()+1;
    let d = t.getDate();
    return y+'.'+m+'.'+d+'';
  }
}
